<%--
  Created by IntelliJ IDEA.
  User: JYC
  Date: 2022/4/14
  Time: 15:17:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>123</title>
</head>
<body>
<div class="easyui-layout" style="width:100%;height: 100%">
    <div region="north" title="搜索客户" style="height:85px;">
        <div class="easyui-panel" style="width: 100%;padding: 10px 60px" align="center">
            <input class="easyui-searchbox" data-options="prompt:'请输入客户编号...',searcher:doSearch" style="width:300px">
        </div>
    </div>
    <div id="content_center" region="center" title="可用车辆列表" style="padding:5px;">
        <table class="easyui-datagrid" id="rent_dg_tb"></table>
    </div>
    <div id="toolbar4">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newRent()">新增租车业务</a>
    </div>
    <div id="new_rent_dg" class="easyui-dialog" style="width: 500px"
         data-options="closed:true,modal:true,border:'thin',buttons:'#bts_yn'">
        <form id="new_rent_form" method="post" novalidate style="margin: 0;padding: 10px 50px">
            <div style="margin-bottom:10px">
                <input name="rentid" class="easyui-textbox" readonly="readonly" style="width: 100%" label="出租单号：">
            </div>
            <div style="margin-bottom:10px">
                <input name="price" class="easyui-textbox" style="width: 100%" label="车辆租金：">
            </div>
            <div style="margin-bottom:10px">
                <input name="begindate" class="easyui-datetimebox" data-options="formatter:myformatter,parser:myparser"
                       style="width: 100%" label="起租日期：">
            </div>
            <div style="margin-bottom:10px">
                <input name="returndate" class="easyui-datetimebox" data-options="formatter:myformatter,parser:myparser"
                       style="width: 100%" label="归还日期：">
            </div>
            <div style="margin-bottom:10px">
                <input name="identity" class="easyui-textbox" readonly="readonly" style="width: 100%" label="客户编号：">
            </div>
            <div style="margin-bottom:10px">
                <input name="carnumber" class="easyui-textbox" readonly="readonly" style="width: 100%" label="车辆编号：">
            </div>
            <div style="margin-bottom:10px">
                <input name="opername" class="easyui-textbox" readonly="readonly" style="width: 100%" label="业务经理：">
            </div>
        </form>
    </div>
    <div id="bts_yn">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveRent()"
           style="width: 90px">确认</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"
           onclick="javascript:$('#new_rent_dg').dialog('close')" style="width: 90px">关闭</a>
    </div>
</div>
<script type="text/javascript">
    let identity_s = '';

    function saveRent() {
        $("#new_rent_form").form('submit',{
            url:'addRent',
            onSubmit:function (){
                return $(this).form('validate');
            },
            success:function (res) {
                if (res.errorMsg){
                    $.messager.show({
                            title:'错误',
                            msg:res.errorMsg
                        });
                } else {
                    $('#new_rent_dg').dialog('close');
                    $('#rent_dg_tb').dialog('reload');
                }
            }
            }
        )
    }

    function newRent() {
        let datagrid = $('#rent_dg_tb').datagrid('getSelected');
        if (datagrid) {
            $("#new_rent_dg").dialog('open').dialog('setTitle', '新建租车订单')
            $("#new_rent_form").form('clear');
            $.post('getRentsID', 'identity=' + identity_s + '&carnumber=' + datagrid.carnumber, function (res) {
                $('#new_rent_form').form('load', res);
            }, 'json');
        }
    }

    function doSearch(value) {
        identity_s = value;
        $("#rent_dg_tb").html('');
        // console.log(value)
        $.post('getCust', 'identity=' + value + '&rows=0', function (res) {
            res = JSON.parse(res);
            console.log(res);
            console.log(res.total);
            if (res.total === 1) {
                console.log('客户存在！')
                loadCarList2();
            } else {
                $.messager.show({
                    title: '提示',
                    msg: value + '：该客户不存在！！！'
                })
            }
        })
    }

    function loadCarList2() {
        $("#rent_dg_tb").datagrid({
                url: 'qryCar?isrenting=0',
                singleSelect: true,
                fitColumns: true,
                fit: true,
                pagination: true,
                rownumbers: true,
                // title: '可用车辆列表',
                iconCls: 'icon-ok',
                toolbar: '#toolbar4',
                columns: [[
                    {field: 'carnumber', title: '车牌号', width: 40},
                    {field: 'cartype', title: '车辆类型', width: 40},
                    {field: 'color', title: '颜色', width: 40, align: 'right'},
                    {field: 'price', title: '价格', width: 80, align: 'right'},
                    {field: 'rentprice', title: '租金', width: 40},
                    {field: 'deposit', title: '押金', width: 60, align: 'center'},
                    {
                        field: 'isrenting', title: '租赁状态', width: 60, align: 'center', formatter: function (value) {
                            return value == 0 ? "未出租" : "已出租"
                        }
                    }
                ]],
                view: detailview,
                detailFormatter: function (rowIndex, rowData) {
                    return '<table><tr><td><img src="' + rowData.carimg + '" style="height: 300px"/></td><td><h3>车辆描述：</h3>"' + rowData.description + '</td></tr></table>'
                }
        })
    }

    function myformatter(value) {
        var date = new Date(value);
        var year = date.getFullYear().toString();
        var month = (date.getMonth() + 1);
        var day = date.getDate().toString();
        var hour = date.getHours().toString();
        var minutes = date.getMinutes().toString();
        var seconds = date.getSeconds().toString();
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        if (hour < 10) {
            hour = "0" + hour;
        }
        if (minutes < 10) {
            minutes = "0" + minutes;
        }
        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        return year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + seconds;
    }

    function myparser(s) {
        if (!s) return new Date();
        var y = s.substring(0, 4);
        var m = s.substring(5, 7);
        var d = s.substring(8, 10);
        var h = s.substring(11, 14);
        var min = s.substring(15, 17);
        var sec = s.substring(18, 20);
        if (!isNaN(y) && !isNaN(m) && !isNaN(d) && !isNaN(h) && !isNaN(min) && !isNaN(sec)) {
            return new Date(y, m - 1, d, h, min, sec);
        } else {
            return new Date();
        }
    }
</script>
</body>
</html>
